<?php

class Application_Model_Dao_StockinMapper extends Base_Mapper
{

	function __construct() {
		parent::__construct();
		$this->tableName = 'Application_Model_DbTable_Stockin';
	}
   
   
	public function save(Application_Model_BusiInfo_Stockin $m)
	{
		$logger = Zend_Registry::get("logger");	
		$data = array(
			"id" => $m->__get("id"),
			"entrepot_id" => $m->__get("entrepotId"),
			"position" => $m->__get("position"),
			'create_date' => date('Y-m-d H:i:s'),
		);
		$logger->log("Saving Application_Model_Stockin:".print_r($data,true),Zend_log::DEBUG);

		$id = $m->get_id();
		if ($id === null || $id === "") {
			unset($data['id']);
			return $this->getDbTable()->insert($data);
		} else {
			return $this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
   
	public function find($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		return $this->toModel($row);		
	}
   
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {	
			$entry = $this->toModel($row);
			$entries[] = $entry;
		}
		return $entries;
	}
	
	private function toModel($record){
    	return new Application_Model_BusiInfo_Stockin(array(
	    	"id"=>$record["id"],
	    	"entrepotId"=>$record["entrepot_id"],
	    	"position"=>$record["position"],
	    	"create_date"=>$record["create_date"],
	    )); 
		
	}
	
	public function delete($id) {
		$where = 'id = ' .  $id;
		$this->getDbTable()->delete($where);
	}
	
	public function getStockinById($stockinId) {
		$db = Zend_Db_Table::getDefaultAdapter();
    	$sql = 'select a.[id] stockin_id, a.[position] position, a.[create_date], b.[id] entrepot_id, b.[address], b.[contact], b.[name], b.[phone]  from stockin a, entrepot b where a.entrepot_id = b.id and  a.id = '.$stockinId.' order by a.create_date';
    	$resultSet = $db->fetchAll($sql);
    	$result =  $resultSet[0];
    	
    	$stockin = new Application_Model_BusiInfo_Stockin(array(
    		'id' => $result['stockin_id'],
    		'position' => $result['position'],
    		'createDate' => $result['create_date'],
    		'entrepotModel' => new Application_Model_BusiInfo_Entrepot(array(
    			'id' => $result['entrepot_id'],
    			'name' => $result['name'],
    			'contact' => $result['contact'],
    	    	'phone' => $result['phone'], 
    			'address' => $result['address'],    	
    		)),
    	));
    	$stockMapper = new Application_Model_Dao_StockMapper();
    	$stockin->__set('stockModelArray', $stockMapper->fetchAll($stockinId));
    	return $stockin;   	
	}
	
	public function getPositionByEntrepotId($eid) {
		$db = Zend_Db_Table::getDefaultAdapter();
    	$sql = 'select a.[position], a.[id]'
    			.'  from stockin a'
    			.' where a.[entrepot_id] = '.$eid;
    	$resultSet = $db->fetchAll($sql);
    	$entries   = array();
		foreach ($resultSet as $row) {	
			$entry = new Application_Model_BusiInfo_Stockin(array(
		    	"id"=>$row["id"],
		    	"position"=>$row["position"],
	   		 ));
			$entries[] = $entry;
		}
		$this->logger->debug(print_r($entries,true));
		return $entries;		
	}
	
	public function getSteelByEntrepotId ($eid) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = 'select c.[steel_id] steel_id,' 
			.'d.name steel_name,'
			.'c.[specs] specs,' 
			.'c.[length] length,'
			.'b.[density] density,'
			.'a.[position] position'
			.' from stockin a, density b, stock c, steel d '
			.' where b.steel_id = c.steel_id'
			.' and b.specs = c.specs'
			.' and a.id = c.[stockin_id]'
			.' and d.id = c.steel_id'
			.' and c.stockin_id = '.$eid
			.' order by d.show_order';
		$this->logger->debug($sql);
    	$resultSet = $db->fetchAll($sql);
//    	$entries   = array();
//		foreach ($resultSet as $row) {	
//			$entry = new Application_Model_Stockin(array(
//		    	"id"=>$row["id"],
//		    	"position"=>$row["position"],
//	   		 ));
//			$entries[] = $entry;
//		}
		$this->logger->debug('getSteelByEntrepotId'.print_r($resultSet,true));
		return $resultSet;		
	}
}

